package com.qf.mywatch.fragment;


import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.qf.mywatch.R;
import com.qf.mywatch.adapter.GridViewAdapter;
import com.qf.mywatch.adapter.ListViewAdapter;
import com.qf.mywatch.adapter.MyPageAdaper;
import com.qf.mywatch.model.Data1;
import com.qf.mywatch.model.Data2;
import com.qf.mywatch.model.Data3;
import com.qf.mywatch.model.News1;
import com.qf.mywatch.model.News2;
import com.qf.mywatch.model.News3;
import com.qf.mywatch.model.Path;
import com.qianfeng.utils.BitMapAsyncTask;
import com.qianfeng.utils.JSONAsyncTask;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;


/**
 * A simple {@link Fragment} subclass.
 * 首页
 */
public class ListViewFragment extends Fragment implements AbsListView.OnScrollListener {

    private Toolbar toolbar;
    private ViewPager viewPager;
    private ImageView iv1;
    private ArrayList<ImageView> list_iv1;
    private MyPageAdaper adapter;
    private ImageView[] ivs1;
    private int[] ivIds1 = {R.id.tou_frag_iv1, R.id.tou_frag_iv2, R.id.tou_frag_iv3};
    private Handler handler1;
    private Timer timer1;
    private int index1;
    private ArrayList<Data1> list1;

    private ListView lv;
    private ListViewAdapter adapter_lv;
    private ArrayList<Data2> list_lv;
    private int index = 1;
    private boolean isJiaZai;

    private GridView Gv2;
    private GridViewAdapter adapter_Gv2;
    private ArrayList<Data3> list_Gv2;

    private ImageView iv0;
    private TextView tv0;
    private ImageView iv01;
    private TextView tv01;
    private TextView tv011;
    private ImageView iv02;
    private TextView tv02;
    private TextView tv021;

    private ArrayList<Data3> list_lv0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.listview, null);
        initView(view);
        toolbarView();
        initV1(view);
        initGv2(view);
        initLv(view);
        initLv0(view);
        return view;
    }

    private void initLv0(View view) {
        iv0= (ImageView) view.findViewById(R.id.tou_frag_iv0);
        tv0= (TextView) view.findViewById(R.id.tou_frag_tv0);
        iv01= (ImageView) view.findViewById(R.id.tou_frag_iv01);
        tv01= (TextView) view.findViewById(R.id.tou_frag_tv01);
        tv011= (TextView) view.findViewById(R.id.tou_frag_tv011);
        iv02= (ImageView) view.findViewById(R.id.tou_frag_iv02);
        tv02= (TextView) view.findViewById(R.id.tou_frag_tv02);
        tv021= (TextView) view.findViewById(R.id.tou_frag_tv021);
        list_lv0 = new ArrayList<>();
        loadData0(view);
    }

    private void loadData0(View convertView) {

        new JSONAsyncTask(convertView.getContext(), new JSONAsyncTask.CallBack() {
            @Override
            public void sendStr(String s) {
                News3 news3 = new Gson().fromJson(s, News3.class);
                list_lv0.addAll(news3.result);
                tv0.setText(news3.result.get(0).title);
                tv01.setText(news3.result.get(1).title);
                tv011.setText(news3.result.get(1).subtitle);
                tv02.setText(news3.result.get(2).title);
                tv021.setText(news3.result.get(2).subtitle);
                new BitMapAsyncTask(getActivity(), new BitMapAsyncTask.CallBack() {
                    @Override
                    public void senBit(Bitmap bitmap, String s) {
                        iv0 = new ImageView(getActivity());
                        iv0.setImageBitmap(bitmap);
                    }
                }).execute(list_lv0.get(0).img);
                new BitMapAsyncTask(getActivity(), new BitMapAsyncTask.CallBack() {
                    @Override
                    public void senBit(Bitmap bitmap, String s) {
                        iv01 = new ImageView(getActivity());
                        iv01.setImageBitmap(bitmap);
                    }
                }).execute(list_lv0.get(1).img);
                new BitMapAsyncTask(getActivity(), new BitMapAsyncTask.CallBack() {
                    @Override
                    public void senBit(Bitmap bitmap, String s) {
                        iv02 = new ImageView(getActivity());
                        iv02.setImageBitmap(bitmap);
                    }
                }).execute(list_lv0.get(2).img);
            }
        }).execute(Path.HOME_AREA);
    }

    private void toolbarView() {
        toolbar.setBackgroundColor(Color.BLACK);
    }

    //控件实例化
    private void initView(View view) {
        toolbar = (Toolbar) view.findViewById(R.id.listview_frag_tb);
        lv = (ListView) view.findViewById(R.id.listview_frag_lv);
        View touView= LayoutInflater.from(getActivity()).inflate(R.layout.tou,null);
        lv.addHeaderView(touView);
        list_lv = new ArrayList<>();
        adapter_lv = new ListViewAdapter(getActivity(), list_lv);
        lv.setAdapter(adapter_lv);
        lv.setOnScrollListener(this);
        loadData();
    }

    //vp1轮播
    private void initV1(View view) {
        viewPager = (ViewPager) view.findViewById(R.id.tou_frag_vp);
        list_iv1 = new ArrayList<>();//图片
        list1 = new ArrayList<>();//data数据
        new JSONAsyncTask(getActivity(), new JSONAsyncTask.CallBack() {
            @Override
            public void sendStr(String s) {
                News1 news1 = new Gson().fromJson(s, News1.class);//解析vp1数据
                list1.addAll(news1.result);
                for (int i = 0; i < list1.size(); i++) {
                    new BitMapAsyncTask(getActivity(), new BitMapAsyncTask.CallBack() {
                        @Override
                        public void senBit(Bitmap bitmap, String s) {
                            iv1 = new ImageView(getActivity());
                            iv1.setImageBitmap(bitmap);
                            list_iv1.add(iv1);
                            adapter = new MyPageAdaper(getActivity(), list_iv1);
                            viewPager.setAdapter(adapter);
                        }
                    }).execute(list1.get(i).img);
                }
            }
        }).execute(Path.HOME_VIEWPAGER);
        adapter = new MyPageAdaper(list_iv1);
        viewPager.setAdapter(adapter);
        //页面滑动下标点改变
        ivs1 = new ImageView[ivIds1.length];
        for (int i = 0; i < ivIds1.length; i++) {
            ivs1[i] = (ImageView) view.findViewById(ivIds1[i]);
        }
        ivs1[0].setImageResource(R.drawable.poster_blue_point);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                for (int i = 0; i < ivs1.length; i++) {
                    if (i == position) {
                        ivs1[i].setImageResource(R.drawable.poster_blue_point);
                    } else {
                        ivs1[i].setImageResource(R.drawable.poster_red_point);
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //自动播放
        showView();
    }

    //自动播放
    private void showView() {
        handler1 = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (index1 < list_iv1.size() - 1) {
                    index1++;
                } else {
                    index1 = 0;
//                    index--;
                }
                //设置指定的显示页，不需要动画
                viewPager.setCurrentItem(index1, false);
                return true;
            }
        });
        timer1 = new Timer();
        timer1.schedule(new TimerTask() {
            @Override
            public void run() {
                handler1.sendEmptyMessage(10);
            }
        }, 2000, 3000);
    }

    //商品列表
    private void initLv(View view) {


    }

    private void loadData() {

        new JSONAsyncTask(getActivity(), new JSONAsyncTask.CallBack() {
            @Override
            public void sendStr(String s) {
                News2 news = new Gson().fromJson(s, News2.class);
                list_lv.addAll(news.result);
                adapter_lv.notifyDataSetChanged();
            }
        }).execute(Path.getHomeListViewPath(index));

    }

    private void initGv2(View view) {
        Gv2 = (GridView) view.findViewById(R.id.tou_frag_gv);
        list_Gv2 = new ArrayList<>();
        adapter_Gv2 = new GridViewAdapter(getActivity(), list_Gv2);
        Gv2.setAdapter(adapter_Gv2);
        loadDataGv2(view);
    }

    private void loadDataGv2(View convertView) {
        new JSONAsyncTask(convertView.getContext(), new JSONAsyncTask.CallBack() {
            @Override
            public void sendStr(String s) {
                News3 news3 = new Gson().fromJson(s, News3.class);
                list_Gv2.add(news3.result.get(3));
                list_Gv2.add(news3.result.get(4));
                list_Gv2.add(news3.result.get(5));

                adapter_Gv2.notifyDataSetChanged();
            }
        }).execute(Path.HOME_AREA);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (isJiaZai && scrollState == 0) {
                index++;
                loadData();
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        isJiaZai = (firstVisibleItem + visibleItemCount) == totalItemCount;
    }
}
